package org.example;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.example.handler.TimeServerHandler;
import org.example.handler.TimeServerHandlerExecutePool;

import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Time;

/**
 * JDK BIO 通讯模型
 */

public class BioTimeServer {
    @SneakyThrows
    public static void main(String[] args) {
        int port = 9999;
        if (args != null && args.length > 0) {
            port = Integer.valueOf(args[0]);
        }
        ServerSocket server = new ServerSocket(port);

        System.out.println("The time Server is start in prot ：" + port);

        // 使用线程池对此优化独立创建线程优化
        TimeServerHandlerExecutePool singleExecutor = new TimeServerHandlerExecutePool(50, 1000);

        while (true) {
            // 线程阻塞到此处，等待socket被连接后执行
            // 有数据可读，可用数据已经读取完毕，发送异常
            Socket accept = server.accept();
            // 当存在连接时创建线程处理
            // new Thread(new TimeServerHandler(accept)).start();

            singleExecutor.execute(new TimeServerHandler(accept));

        }
    }
}
